<template>
  <div>
    <el-table :data="borrowInfos" border style="width: 100%">
      <el-table-column prop="id" label="序号" width="180"> </el-table-column>
      <el-table-column prop="userInfo.name" label="借款人姓名" width="180">
      </el-table-column>
      <el-table-column prop="userInfo.mobile" label="手机"> </el-table-column>
      <el-table-column prop="amount" label="借款金额"> </el-table-column>
      <el-table-column label="借款期限">
        <template slot-scope="scope">{{ scope.row.period }}个月</template>
      </el-table-column>
      <el-table-column prop="returnMethodStr" label="还款方式">
      </el-table-column>
      <el-table-column prop="moneyUseStr" label="资金用途"> </el-table-column>
      <el-table-column label="年化利率">
        <template slot-scope="scope">
          {{ scope.row.borrowYearRate * 100 }}%
        </template>
      </el-table-column>
      <el-table-column prop="statusStr" label="状态"> </el-table-column>
      <!-- <el-table-column prop="createTime" label="申请时间"> </el-table-column> -->
      <el-table-column label="申请时间" width="100">
        <template slot-scope="scope">
          {{ scope.row.createTime | formatDate }}
        </template>
      </el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button
            size="mini"
            @click="saveBorrowInfo(scope.$index, scope.row)"
            >查看</el-button
          >
          <el-button
            v-if="scope.row.status === 0"
            type="warning"
            size="mini"
            @click="approvalShow(scope.row)"
          >
            审批
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <el-row>
      <el-col :span="24">
        <el-pagination
          background
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="1"
          :page-sizes="[1, 10, 20]"
          :page-size="1"
          layout="total, sizes, prev, pager, next, jumper"
          :total="borrowInfoTotal"
        >
        </el-pagination>
      </el-col>
    </el-row>
    <!-- 审批对话框 -->
    <el-dialog title="审批" :visible.sync="dialogVisible" width="490px">
      <el-form label-position="right" label-width="100px">
        <el-form-item label="是否通过">
          <el-radio-group v-model="borrowInfoApproval.status">
            <el-radio :label="2">通过</el-radio>
            <el-radio :label="-1">不通过</el-radio>
          </el-radio-group>
        </el-form-item>

        <el-form-item v-if="borrowInfoApproval.status == 2" label="标的名称">
          <el-input v-model="borrowInfoApproval.title" />
        </el-form-item>

        <el-form-item v-if="borrowInfoApproval.status == 2" label="起息日">
          <el-date-picker
            v-model="borrowInfoApproval.lendStartDate"
            placeholder="选择开始时间"
            value-format="yyyy-MM-dd"
          />
        </el-form-item>

        <el-form-item v-if="borrowInfoApproval.status == 2" label="年化收益率">
          <el-input v-model="borrowInfoApproval.lendYearRate">
            <template slot="append">%</template>
          </el-input>
        </el-form-item>

        <el-form-item v-if="borrowInfoApproval.status == 2" label="服务费率">
          <el-input v-model="borrowInfoApproval.serviceRate">
            <template slot="append">%</template>
          </el-input>
        </el-form-item>

        <el-form-item v-if="borrowInfoApproval.status == 2" label="标的描述">
          <el-input v-model="borrowInfoApproval.lendInfo" type="textarea" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false"> 取消 </el-button>
        <el-button type="primary" @click="approvalSubmit"> 确定 </el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import borrowInfoApi from "@/api/borrowInfo";
import { formatDate } from "@/utils/date"; //转换日期格式

export default {
  data() {
    return {
      borrowInfos: [],
      borrowInfoTotal: 0,
      search: {
        pageNum: 0,
        limit: 1,
        currentPage: 1,
      },
      dialogVisible: false,
      borrowInfoApproval: {
        status: 2,
        serviceRate: 5,
        lendYearRate: 0, //初始化，解决表单中数据修改时无法及时渲染的问题
      },
    };
  },
  methods: {
    page() {
      borrowInfoApi
        .pageBorrowInfo(this.search)
        .then((response) => {
          console.log(response.data);
          this.borrowInfoTotal = response.data.total;
          this.borrowInfos = response.data.list;
        })
        .catch((error) => {
          console.log(error);
        });
    },
    approvalSubmit() {
      if (
        this.borrowInfoApproval.title == null ||
        this.borrowInfoApproval.title == ""
      ) {
        this.$message.error("请输入标的名称！");
        return;
      }
      if (this.borrowInfoApproval.lendStartDate == null) {
        this.$message.error("请选择起息日期！");
        return;
      }
      if (
        this.borrowInfoApproval.lendYearRate == null ||
        this.borrowInfoApproval.lendYearRate == ""
      ) {
        this.$message.error("请输入年化利率！");
        return;
      }
      if (
        this.borrowInfoApproval.serviceRate == null ||
        this.borrowInfoApproval.serviceRate == ""
      ) {
        this.$message.error("请输入服务费率！");
        return;
      }
      if (
        this.borrowInfoApproval.lendInfo == null ||
        this.borrowInfoApproval.lendInfo == ""
      ) {
        this.$message.error("请输入标的描述！");
        return;
      }
      borrowInfoApi.approval(this.borrowInfoApproval).then(response => {
        this.dialogVisible = false
        this.$message.success(response.data.message)
        this.page(this.search)
      })
    },
    handleSizeChange(val) {
      this.search.limit = val;
      this.page();
    },
    handleCurrentChange(val) {
      this.search.pageNum = val;
      this.search.currentPage = val;
      this.page();
    },
    saveBorrowInfo(index, row) {
      borrowInfoApi.saveBorrowInfo(row).then((response) => {
        this.$router.push("/borrowInfoDetail");
      });
    },
    approvalShow(row) {
      this.dialogVisible = true;
      this.borrowInfoApproval.lendYearRate = row.borrowYearRate * 100;
      this.borrowInfoApproval.id = row.id;
    },
  },
  created() {
    console.log("初始化数据！");
    this.page(this.search);
  },
  filters: {
    formatDate(time) {
      var date = new Date(time);
      return formatDate(date, "yyyy-MM-dd");
    },
  },
};
</script>


</style>

